import  tensorflow as tf
from  tensorflow import  keras

from tensorflow.keras import  layers
encode_input = keras.Input(shape=(28, 28, 1), name='img')

h1 = layers.Conv2D(16, 3, activation=keras.activations.relu)(encode_input)
h2 = layers.Conv2D(32, 3, activation=keras.activations.relu)(h1)
h3 = layers.MaxPool2D(3)(h2)
h4 = layers.Conv2D(32, 3, activation='relu')(h3)
h5 = layers.Conv2D(16, 3, activation='relu')(h4)
encode_output = layers.GlobalMaxPool2D()(h5)

encode_model = keras.Model(inputs= encode_input,outputs= encode_output,name='encode_model')

encode_model.summary()


# 解码器
h2 = layers.Reshape((4, 4, 1))(encode_output)
h2 = layers.Conv2DTranspose(16, 3, activation='relu')(h2)
h2 = layers.Conv2DTranspose(32, 3, activation='relu')(h2)
h2 = layers.UpSampling2D(3)(h2)
h2 = layers.Conv2DTranspose(16, 3, activation='relu')(h2)
decode_output = layers.Conv2DTranspose(1, 3, activation='relu')(h2)

autoencoder = keras.Model(inputs=encode_input, outputs=decode_output, name='autoencoder')
autoencoder.summary()


autoencoder_input = keras.Input(shape=(28,28,1), name='img')
h3 = encode_model(autoencoder_input)
autoencoder_output = decode_model(h3)
autoencoder = keras.Model(inputs=autoencoder_input, outputs=autoencoder_output,
                          name='autoencoder')
autoencoder.summary()